package com.aquarium.util;

import android.content.SharedPreferences;
import android.util.Log;

public class SPUtils {
	public static String TAG = "SPUtils";
	public static int ERROR = -1;

	public static float getSP(SharedPreferences SP, String key,
			float defaultVlaue) {
		if (SP == null) {
			Log.e(TAG, "SP is null");
			return ERROR;
		}
		return SP.getFloat(key, defaultVlaue);
	}

	public static int getSP(SharedPreferences SP, String key, int defaultVlaue) {
		if (SP == null) {
			Log.e(TAG, "SP is null");
			return ERROR;
		}
		return SP.getInt(key, defaultVlaue);
	}

	public static String getSP(SharedPreferences SP, String key,
			String defaultVlaue) {
		if (SP == null || key == null) {
			Log.e(TAG, "SP or key is null");
			return null;
		}
		String string = SP.getString(key, defaultVlaue);
		if (string == null || string.equals("null")) {
			return null;
		}
		return string;
	}

	public static boolean getSP(SharedPreferences SP, String key,
			Boolean defaultVlaue) {
		if (SP == null) {
			Log.e(TAG, "SP is null");
			return false;
		}
		return SP.getBoolean(key, defaultVlaue);
	}

	public static long getSP(SharedPreferences SP, String key, long defaultVlaue) {
		if (SP == null) {
			Log.e(TAG, "SP is null");
			return ERROR;
		}
		return SP.getLong(key, defaultVlaue);
	}
	
	public static void setSP(SharedPreferences.Editor editor, String key,
			int value) {
		if (editor == null) {
			Log.e(TAG, "SP is null");
			return;
		}
		editor.putInt(key, value);
		editor.commit();
	}

	public static void setSP(SharedPreferences.Editor editor, String key,
			Float value) {
		if (editor == null) {
			Log.e(TAG, "SP is null");
			return;
		}
		editor.putFloat(key, value);
		editor.commit();
	}

	public static void setSP(SharedPreferences.Editor editor, String key,
			Boolean value) {
		if (editor == null) {
			Log.e(TAG, "SP is null");
			return;
		}
		editor.putBoolean(key, value);
		editor.commit();
	}

	public static void setSP(SharedPreferences.Editor editor, String key,
			String value) {
		if (editor == null) {
			Log.e(TAG, "SP is null");
			return;
		}
		editor.putString(key, value);
		editor.commit();
	}
	
	public static void setSP(SharedPreferences.Editor editor, String key,
			long value) {
		if (editor == null) {
			Log.e(TAG, "SP is null");
			return;
		}
		editor.putLong(key, value);
		editor.commit();
	}

}
